﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VehicleManager.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the  interface.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using Common;
    using Data.Interfaces;
    using Entities;
    using Interfaces;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Business.VehicleManager"/> type.
    /// </summary>
    public class VehicleManager : IVehicleManager
    {
        /// <summary>
        /// Vehicle repository
        /// </summary>
        private readonly IVehicleRepository _vehicleRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleManager"/> class.
        /// </summary>
        public VehicleManager()
            : this(ServiceLocator.Current.GetInstance<IVehicleRepository>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleManager"/> class.
        /// </summary>
        /// <param name="vehicleRepository">
        /// The resolve.
        /// </param>
        public VehicleManager(IVehicleRepository vehicleRepository)
        {
            Contract.Requires<ArgumentNullException>(vehicleRepository != null);
            _vehicleRepository = vehicleRepository;
        }

        /// <summary>
        /// Gets the filtered.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IEnumerable<Vehicle> List(Vehicle filter, int? pageNo, int? pageSize)
        {
            return _vehicleRepository.Query(filter.Map<Vehicle, Data.Entities.Vehicle>(), pageNo, pageSize)
                .ToList()
                .Select(x => x.Map<Data.Entities.Vehicle, Vehicle>());
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>Id of vehicle added</returns>
        public int Add(Vehicle entity)
        {
            return _vehicleRepository.Add(entity.Map<Vehicle, Data.Entities.Vehicle>());
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(Vehicle entity)
        {
            _vehicleRepository.Update(entity.Map<Vehicle, Data.Entities.Vehicle>());
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(Vehicle entity)
        {
            _vehicleRepository.Delete(entity.Map<Vehicle, Data.Entities.Vehicle>());
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The vehicle id.</param>
        /// <returns>
        /// Vehicle matching the id
        /// </returns>
        public Vehicle GetById(int id)
        {
            return _vehicleRepository.FindById(id).Map<Data.Entities.Vehicle, Vehicle>();
        }
    }
}